﻿using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ToolUtil;
using zyconfig;

namespace zyui
{
    public partial class XsSearch : UserControl
    {
        public List<string> DbKayList;
        public int _kjm;
        public event btnclick BtnScarchClick;
        public string Sql = string.Empty;
        public XsSearch()
        {
            InitializeComponent();
        }
        public void AddConfig()
        {
            this.AddDbList("图书");
            this.jstjKay.SelectedIndex = 0;
            DateTime year = DateTime.Now.AddDays(-180);
            DateChoser.AddTo(this.sTime, year);
            DateChoser.AddTo(this.eTime);
        }
        public void AddDbList(string scdm)
        {
            this.skKay.Items.Clear();
            this.DbKayList = new List<string>();
            this.DbKayList.Clear();
            using (DbOperHandler _open = new Common().Doh())
            {
                string sql = "select * from 书目数据库定义 where  文献类型='" + scdm + "' and 不发布='0' order by 库键码";
                _open.Reset();
                _open.SqlCmd = sql;
                DataTable dt = _open.GetDataTable();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow dRow in dt.Rows)
                    {
                        this.skKay.Items.Add(dRow["书目库名"].ToString().Trim());
                        this.DbKayList.Add(dRow["库键码"].ToString());
                    }
                }
                if (this.skKay.Items.Count > 0)
                {
                    this.skKay.SelectedIndex = 0;
                    this._kjm = int.Parse(this.DbKayList[0].ToString());
                }
            }
        }
        private void skKay_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedIndex = this.skKay.SelectedIndex;
            this._kjm = int.Parse(this.DbKayList[selectedIndex].ToString());
        }        
        public delegate void btnclick(object sender, EventArgs e);

        private void myButton1_Click(object sender, EventArgs e)
        {
            bool flag = false;
            using (DbOperHandler _open = new Common().Doh())
            {
                if (_open.dbType.ToString().Equals("ORACLE") || _open.odbType.ToString().Equals("ORACLE"))
                    flag = true;
                string barCode = this.jsnrTxt.Text.Trim();
                string str = this.jstjKay.Text.Trim();
                string str3 = " ";
                if (!string.IsNullOrEmpty(barCode))
                {
                    if (str == "标准编码")
                    {
                        barCode = barCode.Replace("-", "");
                        if (barCode.Length == 13)
                        {
                            barCode = StringUtil.BarCodeToISBN(barCode);
                        }
                        barCode = StringUtil.BuingISBNSept(barCode);
                    }
                    barCode = new System.Text.RegularExpressions.Regex("[\\s]+").Replace(barCode, " ");
                    barCode = barCode.Trim();//
                    barCode = SqlText.BuindPosSql(barCode.ToUpper());
                    //barCode = StringUtil.ToDBC(barCode);
                    string[] sArray = barCode.Split(',');
                    string Type = this.jsfsKay.Text.Trim();
                    if (sArray.Length == 1)
                    {
                        if (Type == "前方一致")
                        {
                            if (flag)
                            {
                                barCode = ChineseConverter.Convert(barCode, ChineseConversionDirection.SimplifiedToTraditional);
                                str3 = str + "  LIKE UNISTR('" + SqlText.Decompose(barCode) + @"\0025')  ";
                            }
                            else
                            {
                                str3 = str + "  LIKE '" + barCode + "%'  ";
                            }
                        }
                        else if (Type == "中间一致")
                        {
                            if (flag)
                            {
                                barCode = ChineseConverter.Convert(barCode, ChineseConversionDirection.TraditionalToSimplified);
                                str3 = str + @" Like UNISTR('\0025" + SqlText.Decompose(barCode) + @"\0025')  ";
                            }
                            else
                            {
                                str3 = str + " Like '%" + barCode + "%'";
                            }
                        }
                        else if (Type == "后方一致")
                        {
                            if (flag)
                            {
                                barCode = ChineseConverter.Convert(barCode, ChineseConversionDirection.SimplifiedToTraditional);
                                str3 = str + @" Like  UNISTR('\0025" + SqlText.Decompose(barCode) + "')  ";
                            }
                            else
                            {
                                str3 = str + " Like  '%" + barCode + "'";
                            }
                        }
                        else if (Type == "等于")
                        {
                            if (flag)
                            {
                                barCode = ChineseConverter.Convert(barCode, ChineseConversionDirection.SimplifiedToTraditional);
                                str3 = str + " = UNISTR('" + SqlText.Decompose(barCode) + "')  ";
                            }
                            else
                            {
                                str3 = str + " = '" + barCode + "'  ";
                            }
                        }
                        else if (Type == "大于")
                        {
                            if (flag)
                            {
                                barCode = ChineseConverter.Convert(barCode, ChineseConversionDirection.SimplifiedToTraditional);
                                str3 = str + " > UNISTR('" + SqlText.Decompose(barCode) + "')  ";
                            }
                            else
                            {
                                str3 = str + " > '" + barCode + "'  ";
                            }
                        }
                        else if (Type == "小于")
                        {
                            if (flag)
                            {
                                barCode = ChineseConverter.Convert(barCode, ChineseConversionDirection.SimplifiedToTraditional);
                                str3 = str + " < UNISTR('" + SqlText.Decompose(barCode) + "')  ";
                            }
                            else
                            {
                                str3 = str + " < '" + barCode + "'  ";
                            }
                        }
                    }
                    else
                    {
                        if (Type == "介于" && sArray.Length == 2)
                        {
                            str3 += "(" + str + " BETWEEN  ";
                            for (int i = 0; i < sArray.Length; i++)
                            {
                                str3 += "'" + sArray[i] + "' AND ";
                            }
                            str3 = str3.Substring(0, str3.Length - 5);
                            str3 += ") ";
                        }
                    }
                }
                if (string.IsNullOrEmpty(str3.Trim()))
                    str3 = " 键码 is not null ";
                if (!this.bkCheck.Checked)
                    str3 += " and 库键码=" + this._kjm;
                string stime = string.Empty, etime = string.Empty;
                if (this.drqCheck.Checked)
                {
                    stime = this.sTime.Text;
                    etime = this.eTime.Text;
                    str3 += "AND (入库日期 BETWEEN '" + stime + "' AND '" + etime + "')";
                }
                if (!this.gqCheck.Checked)
                    str3 += " and 结束时间>" + DateTime.Now.AddDays(-1).ToString("yyyy-MM-dd") + " ";
                if (this.dshCheck.Checked)
                    str3 += " and 是否送审=1";
                this.Sql = str3;
                if (this.BtnScarchClick != null)
                {
                    this.BtnScarchClick(sender, e);
                }
            }
        }

        private void jstj_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.jsfsKay.Items.Clear();
            if (this.jstjKay.Text.Trim().Equals("单价")){
                this.jsfsKay.Items.Add("等于");
                this.jsfsKay.Items.Add("大于");
                this.jsfsKay.Items.Add("小于");
                this.jsfsKay.Items.Add("介于");
            }
            else
            {
                this.jsfsKay.Items.Add("前方一致");
                this.jsfsKay.Items.Add("中间一致");
                this.jsfsKay.Items.Add("后方一致");
                this.jsfsKay.Items.Add("完全一致");
            }
            this.jsfsKay.SelectedIndex = 0;
        }
    }
}
